package com.store.app.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.MessageAdapter;
import com.store.app.http.Stores;
import com.store.app.http.Submit;
import com.store.app.utils.GsonUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消息
 */
public class MessageActivity extends Activity {
	private Context context;
	private LinearLayout nomessage;
	private ListView listmessage;
	String error;
    List<String> data ;
	MessageAdapter messageadapter;
	Handler  handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法
			super.handleMessage(msg);
			switch (msg.what) {
				case 0:
					if(data.size()==0){
//						nomessage.setVisibility(View.VISIBLE);
//						Toast.makeText(getApplicationContext(),"消息为空",
//								Toast.LENGTH_SHORT).show();
					}else{
						nomessage.setVisibility(View.GONE);
						LocationApplication.getData().addAll(0,data);
						data  = LocationApplication.getData();
						messageadapter = new MessageAdapter(context,data);
						listmessage.setAdapter(messageadapter);
					}
					break;
				case 1:
					Toast.makeText(MessageActivity.this, error,
							Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
			}
		}
	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.message);
		//透明状态栏
//		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		context = this.getApplicationContext();
		ExitApplication.getInstance().addActivity(this);
        listmessage= (ListView) findViewById(R.id.listmessage);
		data  = LocationApplication.getData();
		Log.v("zyl","获取到的data:"+data.size());
		messageadapter = new MessageAdapter(context,data);
		listmessage.setAdapter(messageadapter);
		nomessage= (LinearLayout) findViewById(R.id.mess_nomess);
		findViewById(R.id.retreat).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				finish();
			}
		});
		new token_records().start();
		if(data.size()==0){
			nomessage.setVisibility(View.VISIBLE);
		}else{
			nomessage.setVisibility(View.GONE);
		}
	}
	public class token_records extends Thread {

		public void run() {
			try {
				Thread.sleep(2000);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("app_token", MainActivity.app_token));
				JSONObject paramsone=new JSONObject();

				paramsone.put("member_id", MainActivity.member_id);
				params.add(new BasicNameValuePair("params", paramsone.toString()));
				params.add(new BasicNameValuePair("service", "member.appMsgNotifyFind"));
				String sign=Submit.genPackageSign(params);
				params.add(new BasicNameValuePair("sign", sign));
				JSONObject obj = Submit.submit(params,Stores.http+"/openapi/member");

				String result =  obj.getString("rsp_code");
				if(result.equals("succ")){

					Map<String,Object>resultMap = GsonUtil.jsonToMap(obj.toString());
					data = (List<String>) resultMap.get("data");
					Log.v("zyl","请求成功："+data.size());
					handler.sendEmptyMessage(0);
				}else{
					error = (String) obj.get("error_msg");
					handler.sendEmptyMessage(1);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				handler.sendEmptyMessage(2);
			}
		}
	}
}
